<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.targetId" placeholder="检查目标" clearable>
        <el-option
          v-for="(item,index) in ddlTarget"
          :key="index"
          :label="item.text"
          :value="item.value"
          :selected="item.selected"
        ></el-option>
      </el-select>
      <el-select v-model="listQuery.state" placeholder="使用状态" clearable>
        <el-option label="全选" value="3"></el-option>
        <el-option label="未启用" value="0"></el-option>
        <el-option label="启用" value="1"></el-option>
        <el-option label="废止" value="2"></el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="queryTable">查询</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column align="center" label="编号" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="160px" align="center" label="检查目标">
        <template slot-scope="scope">
          <span>{{ scope.row.dictValue }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="250px" align="center" label="检查表名称">
        <template slot-scope="{row}">{{ row.paperName }}</template>
      </el-table-column>

      <el-table-column min-width="100px" align="center" label="使用状态">
        <template slot-scope="scope">
          <span>{{ scope.row.stateVal }}</span>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="创建时间" min-width="170" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="200" align="center" label="操作" fixed="right">
        <template slot-scope="scope">
          <router-link :to="'/danger/check/preview/'+scope.row.id">
            <el-button type="info" size="small" icon="el-icon-view">预览</el-button>
          </router-link>
          <router-link :to="'/danger/check/update/'+scope.row.id">
            <el-button
              type="success"
              size="small"
              v-if="scope.row.state == 0"
              icon="el-icon-edit"
            >指标</el-button>
          </router-link>
          <el-button
            type="warning"
            size="small"
            icon="el-icon-circle-close"
            v-if="scope.row.state == 1"
            @click="stopTable(scope.row.id)"
          >停用</el-button>
          <el-button
            type="warning"
            size="small"
            icon="el-icon-circle-check"
            v-if="scope.row.state == 0"
            @click="startTable(scope.row.id)"
          >启用</el-button>
          <router-link :to="'/danger/check/update/'+scope.row.id" v-if="scope.row.state == 0">
            <el-button type="primary" size="small" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            v-if="scope.row.state == 0"
            @click="delCheckTable(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageIndex"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  fetchList,
  deleteTable,
  stopCheckTable,
  startCheckTable,
  queryList
} from '@/api/checktable'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { fetchBasics } from '@/api/checkplan'

export default {
  name: 'TaskList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageIndex: 1,
        limit: 10,
        state: '',
        tableName: '',
        targetId: ''
      },

      ddlTarget: []
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.getBasics()
  },
  methods: {
    //基础
    getBasics() {
      this.listLoading = true
      fetchBasics().then((response) => {
        var listData = response.data.ddlTarget

        this.ddlTarget = listData //检查目标
        console.log(this.ddlTarget)
        this.listLoading = false
      })
    },
    //获取列表
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then((response) => {
        console.log(response)
        var listData = response.data.records
        for (let i = 0; i < listData.length; i++) {
          if (listData[i].state == 0) {
            listData[i].stateVal = '未启用'
          } else if (listData[i].state == 1) {
            listData[i].stateVal = '启用'
          } else {
            listData[i].stateVal = '停用'
          }
        }
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    //删除某一张检查表
    delCheckTable(id) {
      console.log(id)
      var parse = {
        cpId: id
      }
      this.$confirm('此操作将永久删除当前检查表, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          //发请求删除
          deleteTable(parse).then((res) => {
            console.log(res.success)
            if (res.success) {
              //删除成功，重新加载数据
              this.getList()
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
            } else {
              this.$message({
                type: 'success',
                message: '删除失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //停用检查表 stopTable
    stopTable(id) {
      console.log(id)
      var parse = {
        cpId: id
      }
      this.$confirm('此操作将永久停用当前检查表, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          //发请求删除
          stopCheckTable(parse).then((res) => {
            if (res.success) {
              //删除成功，重新加载数据
              this.getList()
              this.$message({
                type: 'success',
                message: '停用成功!'
              })
            } else {
              this.$message({
                type: 'success',
                message: '停用失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消停用'
          })
        })
    },
    //启用检查表 stopTable
    startTable(id) {
      console.log(id)
      var parse = {
        cpId: id
      }
      this.$confirm('此操作为启用当前检查表, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          //发请求删除
          startCheckTable(parse).then((res) => {
            if (res.success) {
              //删除成功，重新加载数据
              this.getList()
              this.$message({
                type: 'success',
                message: '启用成功!'
              })
            } else {
              this.$message({
                type: 'success',
                message: '启用失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消启用'
          })
        })
    },
    //条件检索
    queryTable() {
      this.listLoading = true
      // if(this.listQuery.targetId){}
      var parse = {
        applicable_object: Number(this.listQuery.targetId),
        limit: this.listQuery.limit,
        pageIndex: this.listQuery.pageIndex,
        state: this.listQuery.state,
        paper_name: this.listQuery.tableName
      }
      if (parse.applicable_object == '') {
        delete parse.applicable_object
      }
      if (parse.state == '3') {
        parse.state = ''
        if (parse.state == '') {
          delete parse.state
        }
      }
      if (parse.paper_name == '') {
        delete parse.paper_name
      }
      console.log(parse)
      queryList(parse).then((response) => {
        console.log(response)
        var listData = response.data.records
        for (let i = 0; i < listData.length; i++) {
          if (listData[i].state == 0) {
            listData[i].stateVal = '未启用'
          } else if (listData[i].state == 1) {
            listData[i].stateVal = '启用'
          } else {
            listData[i].stateVal = '废止'
          }
        }
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped  lang="scss" >
@import '~@/styles/mixin.scss';
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
.pagination-container {
  padding: 0;
}
.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}
</style>
